<?php
require_once 'Sabai.php';
require_once 'Sabai/Application/Model.php';

define('XIGG_ROLE_ADMIN', 1);
define('XIGG_ROLE_MODERATOR', 2);
define('XIGG_ROLE_EDITOR', 3);
define('XIGG_ROLE_WRITER', 4);
define('XIGG_ROLE_SUBMITTER', 5);

class Xigg extends Sabai_Application_Model
{
    var $_pluginDir = array();
    var $_cacheDir;

    function Xigg()
    {
        parent::Sabai_Application_Model('Xigg', dirname(__FILE__) . '/Xigg');

        $this->setDBPrefix('xigg_');
        $this->setDBScheme('mysql');
        $this->setDBOptions(array('host'      => 'localhost:3307',
                                  'dbname'    => 'xigg',
                                  'user'      => 'root',
                                  'pass'      => '',
                                  'charset'   => '',
                                  'collation' => ''));
        $this->setConfig('siteTitle',  'My website');
        $this->setConfig('siteDescription', 'Powered by Xigg');
        $this->setConfig('numberOfNodesOnTop', 10);
        $this->setConfig('numberOfCommentsOnPage', 20);
        $this->setConfig('numberOfTrackbacksOnPage', 20);
        $this->setConfig('numberOfVotesOnPage', 20);
        $this->setConfig('guestCommentsAllowed', false);
        $this->setConfig('userCommentEditTime', 86400);
        $this->setConfig('numberOfVotesForPopular', 10);
        $this->setConfig('useUpcomingFeature', true);
        $this->setConfig('useCommentFeature', true);
        $this->setConfig('useTrackbackFeature', true);
        $this->setConfig('useVotingFeature', true);
        $this->setConfig('defaultNodesPeriod', 'new');
        $this->setConfig('toppageTitle', 'Xigg');
        $this->setConfig('homepageURL', 'Xigg');

        $this->_addRole(XIGG_ROLE_ADMIN, _('Admin'));
        $this->_addRole(XIGG_ROLE_MODERATOR, _('Moderator'));
        $this->_addRole(XIGG_ROLE_EDITOR, _('Editor'));
        $this->_addRole(XIGG_ROLE_WRITER, _('Writer'));
        $this->_addRole(XIGG_ROLE_SUBMITTER, _('Submitter'));

        //$this->debug();
    }

    function &getInstance()
    {
        $instance =& Sabai_Application::getInstance('Xigg');
        return $instance;
    }

    function setCacheDir($cacheDir)
    {
        $this->_cacheDir = $cacheDir;
    }

    function getCacheDir()
    {
        return $this->_cacheDir;
    }

    function addPluginDir($pluginDir)
    {
        $this->_pluginDir[] = $pluginDir;
    }

    function getPluginDir()
    {
        return $this->_pluginDir;
    }

    /**
     * Gets an instance of HTMLPurifier
     *
     * @return HTMLPurifier
     */
    function &_getHTMLPurifier()
    {
        $this->_verifyCacheDir();
        require_once 'HTMLPurifier.php';
        $config = HTMLPurifier_Config::createDefault();
        $config->autoFinalize = false;
        $config->set('Cache', 'SerializerPath', $this->_cacheDir . '/HTMLPurifier');
        $config->set('Attr', 'EnableID', true);
        //$config->set('Core', 'Encoding', SABAI_CHARSET);
        // remove the port part
        if ($port_pos = strpos(SABAI_URL_HOST, ':')) {
        	$config->set('URI', 'Host', substr(SABAI_URL_HOST, 0, $port_pos));
        } else {
        	$config->set('URI', 'Host', SABAI_URL_HOST);
        }
        $config->set('URI', 'DisableExternalResources', true);
        $config->set('Output', 'TidyFormat', true);
        $config->set('HTML', 'DefinitionID', 'lek');
        $config->set('HTML', 'DefinitionRev', 1);
        $htmlDef =& $config->getHTMLDefinition(true);
        $htmlDef->addAttribute('a', 'rel', 'CDATA');
        $plugin_manager =& $this->getService('PluginManager');
        $plugin_manager->dispatch('HTMLPurifierConfigure', array(&$config));
        $HTMLPurifier =& new HTMLPurifier($config);
        $plugin_manager->dispatch('HTMLPurifierInit', array(&$HTMLPurifier));
        return $HTMLPurifier;
    }

    /**
     * Gets an instance of caching object
     *
     * @return HTMLPurifier
     */
    function &_getCacher()
    {
        $this->_verifyCacheDir();
        require_once 'Cache/Lite.php';
        $options = array('cacheDir' => $this->_cacheDir . '/Cache_Lite/');
        $cacher =& new Cache_Lite($options);
        return $cacher;
    }

    /**
     * Gets an instance of plugin manager
     *
     * @return Xigg_PluginManager
     */
    function &_getPluginManager()
    {
        require_once $this->_path . '/PluginManager.php';
        $this->_pluginDir[] = $this->_path . '/plugins';
        $plugin_mngr =& new Xigg_PluginManager('Xigg', $this, $this->_pluginDir, 'Xigg_Plugin_');
        $plugin_mngr->loadPlugins(false);
        return $plugin_mngr;
    }

    /**
     * Gets an instance of tag cloud builder
     *
     * @return Xigg_TagCloudBuilder
     */
    function &_getTagCloudBuilder()
    {
        require_once $this->_path . '/TagCloudBuilder.php';
        $tag_cloud =& new Xigg_TagCloudBuilder($this->getService('Model'), $this->getService('Cacher'));
        return $tag_cloud;
    }

    function _verifyCacheDir()
    {
        if (empty($this->_cacheDir) || !is_dir($this->_cacheDir)) {
            die('Invalid cache directory. Please set the correct cache directory using Xigg:setCacheDir()');
        }
        if (!is_writable($this->_cacheDir)) {
            die(sprintf('All the folders under the cache directory %s must be configured writeable by the server.', SABAI_URL_DIR . '/cache'));
        }
    }
}